{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if One String Swap Can Make Strings Equal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: areAlmostEqual"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #仅执行一次字符串交换能否使两个字符串相等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你长度相等的两个字符串 <code>s1</code> 和 <code>s2</code> 。一次<strong> 字符串交换 </strong>操作的步骤如下：选出某个字符串中的两个下标（不必不同），并交换这两个下标所对应的字符。</p>\n",
    "\n",
    "<p>如果对 <strong>其中一个字符串</strong> 执行 <strong>最多一次字符串交换</strong> 就可以使两个字符串相等，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s1 = \"bank\", s2 = \"kanb\"\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>例如，交换 s2 中的第一个和最后一个字符可以得到 \"bank\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s1 = \"attack\", s2 = \"defend\"\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>一次字符串交换无法使两个字符串相等\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s1 = \"kelb\", s2 = \"kelb\"\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>两个字符串已经相等，所以不需要进行字符串交换\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>s1 = \"abcd\", s2 = \"dcba\"\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s1.length, s2.length &lt;= 100</code></li>\n",
    "\t<li><code>s1.length == s2.length</code></li>\n",
    "\t<li><code>s1</code> 和 <code>s2</code> 仅由小写英文字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-one-string-swap-can-make-strings-equal](https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-one-string-swap-can-make-strings-equal](https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"bank\"\\n\"kanb\"', '\"attack\"\\n\"defend\"', '\"kelb\"\\n\"kelb\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        ans = 0\n",
    "        if Counter(s1) != Counter(s2):\n",
    "            return False\n",
    "        for i in range(len(s1)):\n",
    "\n",
    "            if s1[i] != s2[i]:\n",
    "                ans += 1\n",
    "                if ans >2:\n",
    "                    return False\n",
    "        if ans == 2 or ans == 0:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        l1, l2 = len(s1), len(s2)\n",
    "        if l1 != l2:return False\n",
    "        diffs = []\n",
    "        for i in range(l1):\n",
    "            if s1[i] != s2[i]:\n",
    "                diffs.append((s1[i], s2[i]))\n",
    "        if len(diffs) == 1 or len(diffs) > 2: return False\n",
    "        if len(diffs) == 0: return True\n",
    "        return diffs[0][0]==diffs[1][1] and diffs[0][1] == diffs[1][0]\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # diffs = []\n",
    "        # for i in range(len(s1)):\n",
    "        #     if s1[i] != s2[i]:\n",
    "        #         diffs.append((s1[i], s2[i]))\n",
    "        # if len(diffs) > 2 or len(diffs) == 1:\n",
    "        #     return False\n",
    "        # if len(diffs) == 0:\n",
    "        #     return True\n",
    "        # return diffs[0][0]==diffs[1][1] and diffs[0][1] == diffs[1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        num=0\n",
    "        res=[]\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i]!=s2[i]:\n",
    "                num=num+1\n",
    "                res.append(s1[i])\n",
    "                res.append(s2[i])\n",
    "        return len(res)<=4 and res==res[::-1] \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        n, i, l = 0, 0, len(s1)\n",
    "        x = []\n",
    "        while n < 3 and i < l:\n",
    "            if s1[i] != s2[i]:\n",
    "                n += 1\n",
    "                x.append((s1[i], s2[i]))\n",
    "                if n > 2:\n",
    "                    return False\n",
    "            i += 1\n",
    "        return n == 0 or n == 2 and x[0][0] == x[1][1] and x[0][1] == x[1][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        cnt =0\n",
    "        c1=c2=None\n",
    "        for a,b in zip(s1,s2):\n",
    "            if a!=b: \n",
    "                cnt+=1\n",
    "                if cnt>2 or (cnt ==2 and (a !=c2 or b != c1)):\n",
    "                        return False\n",
    "                c1,c2= a,b\n",
    "        return cnt !=1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        #   通过counter函数，统计字符串中每个字符出现的次数\n",
    "        dic1 = collections.Counter(s1)\n",
    "        dic2 = collections.Counter(s2)\n",
    "        #   如果不同，则返回 false\n",
    "        if dic1 != dic2:\n",
    "            return False\n",
    "        #   如果长度不同，则返回 false\n",
    "        if len(s1) != len(s2):\n",
    "            return False\n",
    "\n",
    "        cnt = 0\n",
    "        #   最多只会出现两个位置的字符不同\n",
    "        for i, j in zip(s1, s2):\n",
    "            if i != j:\n",
    "                cnt += 1\n",
    "\n",
    "        if cnt <= 2:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        ad={}\n",
    "        if s1==s2:\n",
    "            return True\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i]!=s2[i] :\n",
    "                if s1[i] not in ad:\n",
    "                    ad[s1[i]]=s2[i]\n",
    "                else:\n",
    "                    return False\n",
    "            \n",
    "        al=[x for x in ad]\n",
    "        if len(al)==2 and ad[al[0]]==al[-1] and ad[al[1]]==al[0]:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        a = 0\n",
    "        m, n = \"\", \"\"\n",
    "        if set(s1) != set(s2):\n",
    "            return False\n",
    "        else:\n",
    "            for i in range(len(s1)):\n",
    "                if a == 1 and s1[i] != s2[i] and (m != s2[i] or n != s1[i]):\n",
    "                    return False\n",
    "                if s1[i] != s2[i]:\n",
    "                    m, n = s1[i], s2[i]\n",
    "                    a += 1\n",
    "                if a > 2:\n",
    "                    return False\n",
    "        return a != 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        idx=[]\n",
    "        for i in range(0,len(s1)):\n",
    "            if(s1[i]!=s2[i]):\n",
    "                idx.append(i)\n",
    "        if (len(idx)==0):\n",
    "            return True\n",
    "        elif (len(idx)==2):\n",
    "            if (s1[idx[0]]==s2[idx[1]]) & (s1[idx[1]]==s2[idx[0]]):\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        i=j=-1\n",
    "        for idx,(x,y) in enumerate(zip(s1,s2)):\n",
    "            if x!=y:\n",
    "                if i<0:\n",
    "                    i=idx\n",
    "                elif j<0:\n",
    "                    j=idx\n",
    "                else:\n",
    "                    return False\n",
    "        return i<0 or j>=0 and s1[i]==s2[j] and s1[j]==s2[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        counter=0\n",
    "        data1=[]\n",
    "        data2=[]\n",
    "        if s1==s2:\n",
    "            return True\n",
    "        else:\n",
    "            for i in range(len(s1)):\n",
    "                if s1[i]!=s2[i]  :\n",
    "                    data1.append(s1[i])\n",
    "                    data2.append(s2[i])\n",
    "            if len(data1)==2 and data1[0]==data2[1] and data1[1]==data2[0]:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "                   \n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        if s1 == s2: return True\n",
    "        s = list(s1)\n",
    "        for i in range(len(s1)):\n",
    "            for j in range(i + 1, len(s1)):\n",
    "                s[i], s[j] = s[j], s[i]\n",
    "                if ''.join(s) == s2:\n",
    "                    return True\n",
    "                s[i], s[j] = s[j], s[i]\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        return sorted(s1) == sorted(s2) and sum(a != b for a, b in zip(s1, s2)) < 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        index = 0\n",
    "        diff_char = []\n",
    "        while index < len(s1):\n",
    "            if s1[index] != s2[index]:\n",
    "                diff_char.append([s1[index], s2[index]])\n",
    "            if len(diff_char) > 2:\n",
    "                return False\n",
    "\n",
    "            index += 1\n",
    "        if len(diff_char) == 0:\n",
    "            return True\n",
    "        elif len(diff_char) == 1:\n",
    "            return False\n",
    "        # print(diff_char)\n",
    "        return diff_char[0][0] == diff_char[1][1] and diff_char[0][1] == diff_char[1][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        st1 = list(s1)\n",
    "        st2 = list(s2)\n",
    "\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "\n",
    "        if len(s1) != len(s2):\n",
    "            return False\n",
    "\n",
    "        index = []\n",
    "        for i in range(len(s1)):\n",
    "            if st1[i] != st2[i]:\n",
    "                index.append(i)\n",
    "        \n",
    "        if len(index) == 2 and st1[index[0]] == st2[index[1]] and st1[index[1]] == st2[index[0]]:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        cnt = 0\n",
    "        c1 = c2 = None\n",
    "        for a, b in zip(s1, s2):\n",
    "            if a != b:\n",
    "                cnt += 1\n",
    "                if cnt > 2 or (cnt == 2 and (a != c2 or b != c1)):\n",
    "                    return False\n",
    "                c1, c2 = a, b\n",
    "        return cnt != 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "        n = len(s1)\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                if s1[:i] + s1[j] + s1[i + 1:j] + s1[i] + s1[j + 1:] == s2:\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "\n",
    "        if sorted(s1) != sorted(s2):\n",
    "            return False\n",
    "\n",
    "        count = 0\n",
    "\n",
    "        for key, value in enumerate(s1):\n",
    "            if value != s2[key]:\n",
    "                count += 1\n",
    "\n",
    "        if count == 2 or count == 0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        lst = []\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] != s2[i]:\n",
    "                lst.append((s1[i], s2[i]))\n",
    "                \n",
    "        if len(lst) != 2:\n",
    "            return False\n",
    "        else:\n",
    "            return False if lst[0] != lst[1][::-1] else True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        m = len(s1)\n",
    "        n = len(s2)\n",
    "\n",
    "        if m != n:\n",
    "            return False\n",
    "        else:\n",
    "            if s1 == s2:\n",
    "                return True\n",
    "            else:\n",
    "                diff_lst = []\n",
    "                a = ''\n",
    "                b = ''\n",
    "                cnt = 0\n",
    "                for i in range(n):\n",
    "                    if s1[i] != s2[i]:\n",
    "                        cnt += 1\n",
    "                        if cnt == 1:\n",
    "                            a = s1[i]\n",
    "                            b = s2[i]\n",
    "\n",
    "                        elif cnt == 2:\n",
    "                            if s1[i] != b or s2[i] != a:\n",
    "                                return False\n",
    "                        else:\n",
    "                            return False\n",
    "                if cnt == 2:\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "\n",
    "                        \n",
    "                    \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        a = 0\n",
    "        # if set(s1) != set(s2):\n",
    "        #     return False\n",
    "        # else:\n",
    "        m, n = \"\", \"\"\n",
    "        for i in range(len(s1)):\n",
    "            if a == 1 and s1[i] != s2[i]:\n",
    "                if m != s2[i] or n != s1[i]:\n",
    "                    return False\n",
    "            if s1[i] != s2[i]:\n",
    "                m = s1[i]\n",
    "                n = s2[i]\n",
    "                a += 1\n",
    "        return a == 0 or a == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        n=len(s1)\n",
    "        temp=[]\n",
    "        for i in range(n):\n",
    "            if s1[i]!=s2[i]:\n",
    "                temp.append(i)\n",
    "        if len(temp)>2:\n",
    "            return False\n",
    "        if len(temp)==0:\n",
    "            return True\n",
    "        if len(temp)==1:\n",
    "            return False\n",
    "        if s1[temp[0]]==s2[temp[-1]] and s1[temp[-1]]==s2[temp[0]]:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        n=len(s1)\n",
    "        s1,s2=list(s1),list(s2)\n",
    "        for i in range(n):\n",
    "            for j in range(i,n):\n",
    "                s1[i],s1[j]=s1[j],s1[i]\n",
    "                if s1==s2:\n",
    "                    return True\n",
    "                s1[i],s1[j]=s1[j],s1[i]\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        a = 0\n",
    "        if set(s1) != set(s2):\n",
    "            return False\n",
    "        else:\n",
    "            m, n = \"\", \"\"\n",
    "            for i in range(len(s1)):\n",
    "                if a == 1 and s1[i] != s2[i]:\n",
    "                    if m != s2[i] or n != s1[i]:\n",
    "                        return False\n",
    "                if s1[i] != s2[i]:\n",
    "                    m = s1[i]\n",
    "                    n = s2[i]\n",
    "                    a += 1\n",
    "        return a == 0 or a == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        ls1, ls2 = [], []\n",
    "        cnt = 0\n",
    "        for i,j in zip(s1,s2):\n",
    "            if i != j:\n",
    "                ls1.append(i)\n",
    "                ls2.append(j)\n",
    "                cnt += 1\n",
    "                if cnt > 2:\n",
    "                    return False\n",
    "        if cnt == 0:\n",
    "            return True\n",
    "        elif cnt == 2 and sorted(ls1) == sorted(ls2):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        # 只存在两次不相同\n",
    "        if s1 == s2 :\n",
    "            return True\n",
    "        pos = 0\n",
    "        diffx , diffy = \"\" , \"\"\n",
    "        work , done = False , False\n",
    "        while pos < len(s1) :\n",
    "            if not done and not work and s1[pos] != s2[pos] :\n",
    "                diffx = s1[pos]\n",
    "                diffy = s2[pos]\n",
    "                work = True \n",
    "            elif not done and work and s1[pos] != s2[pos] :\n",
    "                if s1[pos] == diffy and s2[pos] == diffx :\n",
    "                    done = True\n",
    "                else :\n",
    "                    return False\n",
    "            elif done and s1[pos] != s2[pos] :\n",
    "                return False\n",
    "            pos += 1 \n",
    "        return done"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        diff_count = 0  # 记录字符位置不同的数量\n",
    "        diff_positions = []  # 记录字符位置不同的索引\n",
    "        \n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] != s2[i]:\n",
    "                diff_count += 1  # 发现字符位置不同，数量加1\n",
    "                diff_positions.append(i)  # 记录字符位置不同的索引\n",
    "            \n",
    "            if diff_count > 2:\n",
    "                return False  # 若字符位置不同的数量大于2，则直接返回False\n",
    "        \n",
    "        if diff_count == 0:\n",
    "            return True  # 两个字符串完全相等，返回True\n",
    "        elif diff_count == 2:\n",
    "            # 检查交换两个不同位置的字符后是否相等\n",
    "            return s1[diff_positions[0]] == s2[diff_positions[1]] and s1[diff_positions[1]] == s2[diff_positions[0]]\n",
    "        else:\n",
    "            return False  # 字符位置不同的数量不是0或2，返回False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "        if Counter(s1) != Counter(s2):\n",
    "            return False\n",
    "        cnt = 0\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] != s2[i]:\n",
    "                cnt += 1\n",
    "        return cnt == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        ls1, ls2 = [], []\n",
    "        cnt = 0\n",
    "        for i,j in zip(s1,s2):\n",
    "            if i != j:\n",
    "                ls1.append(i)\n",
    "                ls2.append(j)\n",
    "                cnt += 1\n",
    "                if cnt > 2:\n",
    "                    return False\n",
    "        if cnt == 0:\n",
    "            return True\n",
    "        elif cnt == 2 and sorted(ls1) == sorted(ls2):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        ls1, ls2 = [], []\n",
    "        cnt = 0\n",
    "        for i,j in zip(s1,s2):\n",
    "            if i != j:\n",
    "                ls1.append(i)\n",
    "                ls2.append(j)\n",
    "                cnt += 1\n",
    "                if cnt > 2:\n",
    "                    return False\n",
    "        if cnt == 0:\n",
    "            return True\n",
    "        elif cnt == 2 and sorted(ls1) == sorted(ls2):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        #s1 和s2 diff=2 且相同\n",
    "        diff1 = None\n",
    "        diff2 = None \n",
    "        cnt = 0\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] != s2[i]:\n",
    "                if diff1 and diff2:\n",
    "                    if s1[i]!=diff2 or s2[i]!=diff1:\n",
    "                        return False\n",
    "                cnt += 1\n",
    "                diff1 = s1[i]\n",
    "                diff2 = s2[i]\n",
    "        #如果最后怎么办 \n",
    "        return cnt == 2 or cnt==0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        lst = [[s1[i], s2[i]] for i in range(len(s1)) if s1[i] != s2[i]]\n",
    "        if len(lst) == 0:\n",
    "            return True\n",
    "        elif len(lst) == 2:\n",
    "            return False if lst[0] != lst[1][::-1] else True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "\n",
    "        if sorted(s1) != sorted(s2):\n",
    "            return False\n",
    "\n",
    "        count = 0\n",
    "\n",
    "        for key, value in enumerate(s1):\n",
    "            if value != s2[key]:\n",
    "                count += 1\n",
    "\n",
    "        if count == 2 or count == 0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        ls1, ls2 = [], []\n",
    "        cnt = 0\n",
    "        for i,j in zip(s1,s2):\n",
    "            if i != j:\n",
    "                ls1.append(i)\n",
    "                ls2.append(j)\n",
    "                cnt += 1\n",
    "                if cnt > 2:\n",
    "                    return False\n",
    "        if cnt == 0:\n",
    "            return True\n",
    "        elif cnt == 2 and sorted(ls1) == sorted(ls2):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        t1=list(s1)\n",
    "        t2=list(s2)\n",
    "        if len(t1)!=len(t2):\n",
    "            return False\n",
    "        count=0\n",
    "        res=[]\n",
    "        for i in range(len(t1)):\n",
    "            if t1[i]!=t2[i]:\n",
    "                count+=1\n",
    "                res+=[t1[i],t2[i]]\n",
    "            if count>2:\n",
    "                return False\n",
    "        if count==0:\n",
    "            return True\n",
    "        if count==2:\n",
    "            if [res[0],res[2]]==[res[3],res[1]]:\n",
    "                return True\n",
    "        return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        t1=list(s1)\n",
    "        t2=list(s2)\n",
    "        if len(t1)!=len(t2):\n",
    "            return False\n",
    "        count=0\n",
    "        res=[]\n",
    "        for i in range(len(t1)):\n",
    "            if t1[i]!=t2[i]:\n",
    "                count+=1\n",
    "                res+=[t1[i],t2[i]]\n",
    "        if count==0:\n",
    "            return True\n",
    "        if count==2:\n",
    "            if [res[0],res[2]]==[res[3],res[1]]:\n",
    "                return True\n",
    "        return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "        a = b = -1\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] != s2[i]:\n",
    "                if a == -1:\n",
    "                    a = i\n",
    "                elif b == -1:\n",
    "                    b = i\n",
    "                else:\n",
    "                    return False\n",
    "        return s1[a] == s2[b] and s1[b] == s2[a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        if sorted(s1) != sorted(s2):\n",
    "            return False\n",
    "        return sum(1 for i in range(len(s1)) if s1[i] != s2[i]) in [0,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        diff = 0\n",
    "        last = None\n",
    "        for c1, c2 in zip(s1, s2):\n",
    "            if c1 != c2:\n",
    "                diff += 1\n",
    "                if diff == 1:\n",
    "                    last = c1, c2\n",
    "                elif diff > 2 or diff == 2 and last != (c2, c1):\n",
    "                    return False\n",
    "        return diff != 1\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        lst = []\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] != s2[i]:\n",
    "                lst.append((s1[i], s2[i]))\n",
    "        if len(lst) != 2:\n",
    "            return False\n",
    "        else:\n",
    "            if lst[0] != lst[1][::-1]:\n",
    "                return False\n",
    "            else:\n",
    "                return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "\n",
    "        if sorted(s1) != sorted(s2):\n",
    "            return False\n",
    "        count = 0\n",
    "\n",
    "        for key, value in enumerate(s1):\n",
    "            if value != s2[key]:\n",
    "                count += 1\n",
    "\n",
    "        if count == 2 or count == 0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        c=0\n",
    "        x=[]\n",
    "        for i,j in enumerate(s1):\n",
    "            if s1[i]!=s2[i]:\n",
    "                c+=1\n",
    "                x.append([s1[i],s2[i]])\n",
    "                if c>2:\n",
    "                    return False\n",
    "                    break\n",
    "        else:\n",
    "            if len(x)==1:\n",
    "                return False\n",
    "            elif len(x)==0 or x[0]==x[1][::-1]:\n",
    "                return True                \n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        bj = 0\n",
    "        x = 0\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] != s2[i]:\n",
    "                if x and x != s2[i]+s1[i]:\n",
    "                    return False\n",
    "                x = s1[i]+s2[i]\n",
    "                bj += 1\n",
    "                if bj == 3:\n",
    "                    return False\n",
    "        return bj != 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        i = j = -1\n",
    "        for idx,(x,y) in enumerate(zip(s1,s2)):\n",
    "            if x!=y:\n",
    "                if i<0:i = idx\n",
    "                elif j<0:j = idx\n",
    "                else:return False\n",
    "        return i<0 or j>=0 and s1[i]==s2[j] and s1[j]==s2[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "\n",
    "        if sorted(s1) != sorted(s2):\n",
    "            return False\n",
    "\n",
    "        count = 0\n",
    "\n",
    "        for key, value in enumerate(s1):\n",
    "            if value != s2[key]:\n",
    "                count += 1\n",
    "\n",
    "        if count == 2 or count == 0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "        total = 0\n",
    "        index_arr = list()\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] != s2[i]:\n",
    "                total += 1\n",
    "                index_arr.append(i)\n",
    "        return  True if total == 2 and s1[index_arr[0]] == s2[index_arr[1]] and s1[index_arr[1]] == s2[\n",
    "            index_arr[0]] else False\n",
    "            \n",
    "\n",
    "                    \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        s1 = list(s1)\n",
    "        s2 = list(s2)\n",
    "        list1=[]\n",
    "        if s1==s2:\n",
    "            return True\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i]!=s2[i]:\n",
    "                list1.append(i)\n",
    "        if len(list1)==2:\n",
    "            s1[list1[0]],s1[list1[1]]=s1[list1[1]],s1[list1[0]]\n",
    "            if s1==s2:\n",
    "                return True\n",
    "        return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        n = len(s1)\n",
    "        record = {}\n",
    "        count = 0\n",
    "        for i in range(n):\n",
    "            if s1[i] == s2[i]:\n",
    "                continue\n",
    "            else:\n",
    "                count += 1\n",
    "                if count == 1:\n",
    "                    record[s2[i]] = s1[i]\n",
    "                elif count == 2:\n",
    "                    if s1[i] not in record or record[s1[i]] != s2[i]:\n",
    "                        return False\n",
    "                else:\n",
    "                    return False\n",
    "        if count == 1:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        ans=0\n",
    "        if Counter(s1)!=Counter(s2):\n",
    "            return False\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i]!=s2[i]:\n",
    "                ans+=1\n",
    "        return ans==0 or ans==2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        \n",
    "        if s1 == s2:\n",
    "            return True\n",
    "        n = len(s1)\n",
    "        ind = [i for i in range(n) if s1[i] != s2[i]]\n",
    "        if len(ind) == 2:\n",
    "            i, j = ind[:]\n",
    "            if s1[i]==s2[j] and s1[j] == s2[i]:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        res = 0\n",
    "        for i in range(len(s1)):\n",
    "            if s2[i] != s1[i]:\n",
    "                res += 1\n",
    "\n",
    "\n",
    "        return res == 0 or res == 2 and Counter(s1) == Counter(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        cnt = 0\n",
    "        c1 = c2 = None\n",
    "        for a, b in zip(s1, s2):\n",
    "            if a != b:\n",
    "                cnt += 1\n",
    "                if cnt > 2 or (cnt == 2 and (a != c2 or b != c1)):\n",
    "                    return False\n",
    "                c1, c2 = a, b\n",
    "        return cnt != 1\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''字符串相同，直接返回 True\n",
    "字符出现的频次不同，直接返回 False\n",
    "遍历字符串，记录同位置不相等的字符个数，如果刚好两个不相等，那么交换一次'刚好，返回True，否则就是False'''\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "       if s1==s2:\n",
    "           return True\n",
    "       if Counter(s1) != Counter(s2):\n",
    "            return False\n",
    "       cnt = 0\n",
    "       for i in range(len(s1)):\n",
    "            if s1[i]!=s2[i]:\n",
    "                cnt+=1\n",
    "       return cnt == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        cnt = 0\n",
    "        c1 = c2 =None\n",
    "        for i,j in zip(s1,s2):\n",
    "            if i != j:\n",
    "                cnt += 1\n",
    "                if cnt > 2 or (cnt == 2 and (i != c2 or j != c1)):\n",
    "                    return False\n",
    "                c1,c2 = i,j\n",
    "        return cnt != 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        n, a, b = len(s1), -1, -1\n",
    "        for i in range(n):\n",
    "            if s1[i] == s2[i]:\n",
    "                continue\n",
    "            if a == -1:\n",
    "                a = i\n",
    "            elif b == -1:\n",
    "                b = i\n",
    "            else:\n",
    "                return False\n",
    "        if a == -1:\n",
    "            return True\n",
    "        if b == -1:\n",
    "            return False\n",
    "        return s1[a] == s2[b] and s1[b] == s2[a]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        list1=list(s1)\n",
    "        list2=list(s2)\n",
    "        ans=0\n",
    "        list3=[]\n",
    "        if s1==s2:\n",
    "            return True\n",
    "        if len(s1)==len(s2):\n",
    "            if set(s1)==set(s2):\n",
    "                for i in range(len(s1)):\n",
    "                    if list1[i]!=list2[i]:\n",
    "                        ans+=1\n",
    "                        list3.append(i)\n",
    "                if ans>2:\n",
    "                    return False\n",
    "                elif ans==2:\n",
    "                    if list1[list3[0]]==list2[list3[1]]:\n",
    "                        return True \n",
    "                    else: \n",
    "                        return False\n",
    "                else:\n",
    "                    return False   \n",
    "            else:\n",
    "                return False\n",
    "        else:\n",
    "            return False \n",
    "                \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "\n",
    "        if sorted(s1) != sorted(s2):\n",
    "            return False\n",
    "\n",
    "        count = 0\n",
    "\n",
    "        for key, value in enumerate(s1):\n",
    "            if value != s2[key]:\n",
    "                count += 1\n",
    "\n",
    "        if count == 2 or count == 0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        bj = 0\n",
    "        x = 0\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] != s2[i]:\n",
    "                if x and x != s2[i]+s1[i]:\n",
    "                    return False\n",
    "                x = s1[i]+s2[i]\n",
    "                bj += 1\n",
    "                if bj == 3:\n",
    "                    return False\n",
    "        return bj != 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        ss1 = copy.deepcopy(s1)\n",
    "        ss2 = copy.deepcopy(s2)\n",
    "        s1 = sorted(list(s1))\n",
    "        s2 = sorted(list(s2))\n",
    "        ss1 = list(ss1)\n",
    "        ss2 = list(ss2)\n",
    "\n",
    "        if s1 != s2:\n",
    "            return False\n",
    "        \n",
    "        n = len(s1)\n",
    "\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            if ss1[i] != ss2[i]:\n",
    "                pos = []\n",
    "                for j in range(i,n):\n",
    "                    if ss1[j] == ss2[i]:\n",
    "                        pos.append(j)\n",
    "                if len(pos) == 1:\n",
    "                    temp = ss1[i]\n",
    "                    ss1[i] = ss1[pos[0]]\n",
    "                    ss1[pos[0]] = temp\n",
    "                    ans += 1\n",
    "                else:\n",
    "                    find = False\n",
    "                    for p in pos:\n",
    "                        temp = ss1[i]\n",
    "                        sss1 = copy.deepcopy(ss1)\n",
    "                        sss1[i] = ss1[p]\n",
    "                        sss1[p] = temp\n",
    "                        if sss1 == ss2:\n",
    "                            ans += 1\n",
    "                            find = True\n",
    "                            break\n",
    "                    if not find:\n",
    "                        return False\n",
    "                    else:\n",
    "                        break\n",
    "        return ans <= 1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "\n",
    "        if sorted(s1) != sorted(s2):\n",
    "            return False\n",
    "\n",
    "        count = 0\n",
    "\n",
    "        for key, value in enumerate(s1):\n",
    "            if value != s2[key]:\n",
    "                count += 1\n",
    "\n",
    "        if count == 2 or count == 0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "\n",
    "        if sorted(s1) != sorted(s2):\n",
    "            return False\n",
    "\n",
    "        count = 0\n",
    "\n",
    "        for key, value in enumerate(s1):\n",
    "            if value != s2[key]:\n",
    "                count += 1\n",
    "\n",
    "        if count == 2 or count == 0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        bj = 0\n",
    "        x = 0\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] != s2[i]:\n",
    "                if x and x != s2[i]+s1[i]:\n",
    "                    return False\n",
    "                x = s1[i]+s2[i]\n",
    "                bj += 1\n",
    "                if bj == 3:\n",
    "                    return False\n",
    "        return bj != 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        if s1 == s2:\n",
    "            return True\n",
    "        n = len(s1)\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if s1[i] != s2[i]:\n",
    "                res.append(i)\n",
    "        n1 = len(res)\n",
    "        if n1 != 2:\n",
    "            return False\n",
    "        if s1[res[0]] == s2[res[1]] and s1[res[1]] == s2[res[0]]:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def areAlmostEqual(self, s1: str, s2: str) -> bool:\n",
    "        cnt = 0\n",
    "        c1 = c2 = None\n",
    "        for a, b in zip(s1, s2):\n",
    "            if a != b:\n",
    "                cnt += 1\n",
    "                if cnt > 2 or (cnt == 2 and (a != c2 or b != c1)):\n",
    "                    return False\n",
    "                c1, c2 = a, b \n",
    "        return cnt != 1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
